<template>
  <div>
    <el-drawer
      :title="team_is_add?'增加部门' : '编辑部门'"
      :visible.sync="value.visible"
      direction="rtl"
      :before-close="onClose"
      :wrapperClosable="false"
    >
      <el-row :gutter="15" v-if="!team_is_add">
        <el-col :span="7">
          <span>部门编号</span>
        </el-col>
        <el-col :span="12">
          <el-input v-model="team_data.id" disabled></el-input>
        </el-col>
      </el-row>
           <el-row :gutter="15" >
        <el-col :span="7">
          <span>隶属学院</span>
        </el-col>
        <el-col :span="12">
          <el-input v-model="team_data.collegeName"></el-input>
        </el-col>
      </el-row>
           <el-row :gutter="15" >
        <el-col :span="7">
          <span>部门名称</span>
        </el-col>
        <el-col :span="12">
          <el-input v-model="team_data.teamName"></el-input>
        </el-col>
      </el-row>
      <el-row :gutter="15">
        <el-col :span="7">
          <span>部门负责人</span>
        </el-col>
        <el-col :span="12">
            <el-input v-model="team_data.userName" @blur="onQueryUser"></el-input>
        </el-col>
      </el-row>
      <p v-if="team_is_input_start">
       <el-link type="danger" v-if="!team_is_existence" :underline="false" style="margin: 0 0 10px 20px;">此用户尚未注册,系统会自动创建,默认密码为:123456</el-link>
      </p>
          <el-button type="primary" style="margin-left: 20px;" @click="onOk()">{{this.team_is_add ? '立即创建' : '立即保存' }}</el-button>
          <el-button @click="onClose()">取消</el-button>
      </el-form>
    </el-drawer>
  </div>
</template>
<script>
import { addTeam } from '@/api/requests/team'
import { findByUserName } from '@/api/requests/admin'


export default {
  props: ['value'],
  data () {
    return {
      team_data: {},
      team_is_existence:true,
      team_is_input_start:false,
      team_is_allow_spaceWrite:false,
      team_is_add:false,

    }
  },
  watch: {
    value (val) {
      if (val.visible) {
        this.team_data = JSON.parse(JSON.stringify(val.data))
        this.team_is_existence = true
        if(this.team_data.id == undefined){
          this.team_is_add = true
        }else{
          this.team_is_add = false
        }
        if(this.team_data.spaceWrite == 0){
            this.team_is_allow_spaceWrite = true
        }
      }
    },
  },
  created () {
  },
  methods: {
    onQueryUser(){
      this.team_is_input_start = true
        findByUserName(this.team_data.userName).then(res => {
          if(res.code == 200){
            this.team_is_existence = true
          }else{
            this.team_is_existence = false
          }
          
        })
    },
    onOk(){
      this.team_Save_Team()
      this.onClose()
    },
    onClose () {
      this.$emit('input', {
        visible: false,
        data: {
          over:true
        }
      })
    },
    team_Save_Team(){
      this.team_data.teamRole = 'admin'
      this.team_data.spaceWrite = 0
      addTeam(this.team_data).then(res => {
          if (res.code == 200) {
          let msg = '编辑成功'
          if (this.team_is_add) {
              msg = '添加成功'
          }
          this.$message({
              message: msg,
              type: 'success'
          })
       } else {
          this.$message.error('出现异常,请重试');
        }
      })



    }
  },

}
</script>
<style lang="scss" scoped>
.el-drawer {
    padding: 20px;
    .el-form-item__label {
        width: 100px;
    }
    span {
        margin-left: 20px;
        line-height: 40px;
    }
    .el-row {
        margin-bottom: 10px;
    }
    .el-switch {
        line-height: 40px;
    }
}
</style>
